package com.lxyk.controller.monthly;

import com.lxyk.domain.Course;
import com.lxyk.domain.Job;
import com.lxyk.service.monthly.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName courseController
 * @Description TODO
 * @Author FJQ
 * @Date 2022/4/23 15:39
 * @Version 1.0
 **/

//课程表
@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping("/getCourse")
    public String getCourse(Model model){
        List<Course> courseList = courseService.saveQuery();
        model.addAttribute("courseList",courseList);
        return "course";
    }


    //新增
    @RequestMapping("/insertCourse")
    public String insertCourse(Course course){
        int insertCourse = courseService.insertCourse(course);
        if (insertCourse > 0){
            System.out.println("新增成功！");
        }else {
            System.out.println("新增失败！");
        }
        return "redirect:/course/getCourse";
    }

    //删除
    @RequestMapping("/remove")
    public String delete(HttpServletRequest request, Integer id){
        int deleteByKey = courseService.deleteByKey(id);
        if(deleteByKey > 0){
            request.getSession().setAttribute("msg", "删除成功!");
        }else{
            request.getSession().setAttribute("msg", "删除失败!");
        }
        return "redirect:/course/getCourse";
    }

    //根据id查询
    @RequestMapping("/courseById")
    public String findById(Model model,Integer id){
        Course course = courseService.selectByKey(id);
        model.addAttribute("course",course);
        return "course-update";
    }

    //更新
    @RequestMapping("/courseUpdate")
    public String update(HttpServletRequest request, Course course){
        boolean updateByKey = courseService.updateByKey(course);
        if(updateByKey){
            request.getSession().setAttribute("msg", "修改成功!");
        }else{
            request.getSession().setAttribute("msg", "修改失败!");
        }
        return "redirect:/course/getCourse";
    }
}
